package pt.isel.pdm.yamba;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;

public class StatusUpload extends Service implements Runnable {
	public static String DATA = "SU_EXTRA";
	private final BlockingQueue<Intent> queue = new LinkedBlockingQueue<Intent>();
	protected SharedPreferences prefs;
	private Thread worker;
	public void onCreate() {
		super.onCreate();
		worker = new Thread(this, "StatusUpload worker thread"); 
		worker.start();
	}
	public void run() {
		try {
			while(true){
				Intent intent;
				intent = queue.take();
				try {
					Global.getTwitter().updateStatus(intent.getStringExtra(DATA));
				} catch (Exception e) {
					
				}
			}
		} 
		catch (InterruptedException e){ }
	}
	public int onStartCommand(Intent intent, int flags, int startId) {
		queue.add(intent);
		return START_NOT_STICKY;
	}

	@Override
	public void onDestroy() {
		worker.interrupt();
		super.onDestroy();
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
}